// This is the main DLL file.

#include "stdafx.h"
//#include "dbgmetric.h"
#include "PowerGUIVsx.Utils.SdkHelper.h"
using namespace ArdPowerGuiVsxUtils;
using namespace System::Runtime::InteropServices;

_GUID ToGUID( System::Guid^ guid ) {
   array<Byte>^ guidData = guid->ToByteArray();
   pin_ptr<Byte> data = &(guidData[ 0 ]);

   return *(_GUID *)data;
}

HRESULT SdkHelper::SetSdkMetric(
         System::String^ pszType,
         System::Guid^ guidSection,
         System::String^ pszMetric,
		 System::String^   pszValue,
         bool    fUserSpecific,
         System::String^ pszAltRoot
)
{
	_GUID guidSection2 = ToGUID(guidSection);

	IntPtr ptrType = Marshal::StringToHGlobalUni(pszType);
    LPCWSTR pszType2 = (LPCWSTR)ptrType.ToPointer();

	IntPtr ptrMetric = Marshal::StringToHGlobalUni(pszMetric);
	LPCWSTR pszMetric2 = (LPCWSTR)ptrMetric.ToPointer();

	IntPtr ptrValue = Marshal::StringToHGlobalUni(pszValue);
	LPCWSTR pszValue2 = (LPCWSTR)ptrValue.ToPointer();

	IntPtr ptrAltRoot = Marshal::StringToHGlobalUni(pszAltRoot);
	LPCWSTR ptrAltRoot2 = (LPCWSTR)ptrAltRoot.ToPointer();
    
	HRESULT res = SetMetric(pszType2, guidSection2,  pszMetric2, pszValue2, fUserSpecific, ptrAltRoot2);

	Marshal::FreeHGlobal(ptrType);
	Marshal::FreeHGlobal(ptrMetric);
	Marshal::FreeHGlobal(ptrValue);
	Marshal::FreeHGlobal(ptrAltRoot);

	return res;
}

HRESULT SdkHelper::RemoveSdkMetric(
			 System::String^ pszType,
			 System::Guid^ guidSection,
			 System::String^ pszMetric,
			 System::String^ pszAltRoot
			 )
{
	_GUID guidSection2 = ToGUID(guidSection);

	IntPtr ptrType = Marshal::StringToHGlobalUni(pszType);
    LPCWSTR pszType2 = (LPCWSTR)ptrType.ToPointer();

	IntPtr ptrMetric = Marshal::StringToHGlobalUni(pszMetric);
	LPCWSTR pszMetric2 = (LPCWSTR)ptrMetric.ToPointer();

	IntPtr ptrAltRoot = Marshal::StringToHGlobalUni(pszAltRoot);
	LPCWSTR ptrAltRoot2 = (LPCWSTR)ptrAltRoot.ToPointer();

	HRESULT res = RemoveMetric(pszType2, guidSection2, pszMetric2, ptrAltRoot2);

	Marshal::FreeHGlobal(ptrType);
	Marshal::FreeHGlobal(ptrMetric);
	Marshal::FreeHGlobal(ptrAltRoot);

	return res;
}


HRESULT SdkHelper::SetSdkMetric(
         System::String^ pszType,
         System::Guid^ guidSection,
         System::String^ pszMetric,
		 const DWORD   dwValue,
         bool    fUserSpecific,
         System::String^ pszAltRoot
)
{
	_GUID guidSection2 = ToGUID(guidSection);

	IntPtr ptrType = Marshal::StringToHGlobalUni(pszType);
    LPCWSTR pszType2 = (LPCWSTR)ptrType.ToPointer();

	IntPtr ptrMetric = Marshal::StringToHGlobalUni(pszMetric);
	LPCWSTR pszMetric2 = (LPCWSTR)ptrMetric.ToPointer();

	IntPtr ptrAltRoot = Marshal::StringToHGlobalUni(pszAltRoot);
	LPCWSTR ptrAltRoot2 = (LPCWSTR)ptrAltRoot.ToPointer();
    
	HRESULT res = SetMetric(pszType2, guidSection2,  pszMetric2, dwValue, fUserSpecific, ptrAltRoot2);

	Marshal::FreeHGlobal(ptrType);
	Marshal::FreeHGlobal(ptrMetric);
	Marshal::FreeHGlobal(ptrAltRoot);

	return res;
}